package com.ffyc.resale.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ffyc.resale.dao.CommentDao;
import com.ffyc.resale.dao.ForumDao;
import com.ffyc.resale.dao.UserDao;
import com.ffyc.resale.model.Comment;
import com.ffyc.resale.model.Forum;
import com.ffyc.resale.model.User;
import com.ffyc.resale.util.JWTUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.sound.midi.Soundbank;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 评论信息表业务处理
 **/
@Service
public class ForumService {

    @Autowired
    private ForumDao forumDao;
    @Autowired
    private UserDao userDao;
    @Autowired
    RedisTemplate redisTemplate;

    /**
     * 后台删除
     */
    public void deleteById(Integer id) {
        forumDao.deleteById(id);
    }

    /**
     * 批量删除
     */
    public void deleteBatch(List<Integer> ids) {
        for (Integer id : ids) {
            forumDao.deleteById(id);
        }
    }

    /**
     * 修改
     */
    public void updateById(Forum forum) {
        forumDao.updateById(forum);
    }

    /**
     * 分页查询
     */
    public Page<Forum> selectPage(Forum forum, Integer pageNo, Integer pageSize) {
        Page<Forum> page = Page.of(pageNo,pageSize);
        QueryWrapper<Forum> queryWrapper = new QueryWrapper<Forum>().like(forum.getTitle()!=null, "title", forum.getTitle());
        Page<Forum> p = forumDao.selectPage(page,queryWrapper);
        List<Forum> forumList = p.getRecords();
        for (Forum f : forumList) {
            Integer userId = f.getUserid();
            // 根据用户 ID 查询用户名
            String userName = userDao.selectById(userId).getName();
            f.setUserName(userName);
        }
        return p;
    }

    public Page<Forum> forums(Forum forum, Integer pageNo, Integer pageSize) {
        Page<Forum> page = Page.of(pageNo, pageSize);
        QueryWrapper<Forum> wrapper = new QueryWrapper<Forum>().like(forum.getTitle() != null, "title", forum.getTitle());
        return forumDao.selectForumPageWithUser(page, wrapper);
    }

    public User userInfo(String forumId) {
        User user = forumDao.userInfo(forumId);
        return user;
    }

    public void addForum(Forum forum ,String userToken) {
        User openUser = JWTUtil.getUser(userToken);
        forum.setUserid(openUser.getId());
        forumDao.addForum(forum);
    }

    public Forum findForumInfoById(Integer forumId) {
        Forum forum = forumDao.findForumInfoById(forumId);
        // 查询新闻时，查询redis中帖子的点赞数量，如果有，直接返回，如果没有，初始化一个键值
        ValueOperations valueOperations = redisTemplate.opsForValue();
        if(redisTemplate.hasKey(forumId+":dzcount")){
            Integer newDzCount = (Integer) valueOperations.get(forumId+":dzcount");// 获得点赞数量
            forum.setForumDzCount(newDzCount);
        }else{
            valueOperations.set(forumId+":dzcount", 0);// 没有点赞，初始化0
            forum.setForumDzCount(0);
        }
        if(redisTemplate.hasKey(forumId+":readed")){
            valueOperations.increment(forumId+":readed");// 浏览数量加1
            Integer forumReaded = (Integer) valueOperations.get(forumId+":readed");// 获得点赞数量
            forum.setReaded(forumReaded);
        }else{
            valueOperations.set(forumId+":readed", 1);// 没有点赞，初始化1
            forum.setReaded(0);
        }
        return forum;
    }


    public List<Forum> TopForums(Forum forum) {
        List<Forum> forumList = forumDao.topForums(forum);
        return forumList;
    }
}